package button_tools;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

// The menu bar that extends a JMENUBAR and implements action listeners
public class TMenuBar extends JMenuBar implements ActionListener {

        // separate menu for each type
        // our program has 3 meuns
	private JMenu m_FileMenu;
	private JMenu m_EditMenu;
	private JMenu m_HelpMenu;
        // reference to the settings object
        private TPaintSettings m_Settings;

    // Constructor, takes a TPaintSettings for reference
    // and construct the menu
    public TMenuBar(TPaintSettings settings) {
                m_Settings = settings;
		buildFileMenu();
		buildEditMenu();
		buildHelpMenu();

		this.add(m_FileMenu);
		this.add(m_EditMenu);
		this.add(m_HelpMenu);

	}

        // build the file menu
	private void buildFileMenu() {
            // create the file menu, set name, and mnemonic
		m_FileMenu = new JMenu();
		m_FileMenu.setText("File");
		m_FileMenu.setMnemonic(KeyEvent.VK_F);
                
                // set the "New" option
                // set settings and add it to file menu
		JMenuItem f_NewItem = new JMenuItem();
		f_NewItem.setText("New");
		f_NewItem.setMnemonic(KeyEvent.VK_F);
                f_NewItem.setEnabled(false);
		f_NewItem.addActionListener(this);
		m_FileMenu.add(f_NewItem);
                
                // set the "Save" option
                // set settings and add it to file menu
		JMenuItem f_SaveItem = new JMenuItem();
		f_SaveItem.setText("Save");
		f_SaveItem.setMnemonic(KeyEvent.VK_S);
                f_SaveItem.setActionCommand("Save");
		f_SaveItem.addActionListener(this);
		m_FileMenu.add(f_SaveItem);

                 // set the "Open" option
                // set settings and add it to file menu
                JMenuItem f_OpenItem = new JMenuItem();
		f_OpenItem.setText("Open");
		f_OpenItem.setMnemonic(KeyEvent.VK_O);
                f_OpenItem.setActionCommand("Open");
		f_OpenItem.addActionListener(this);
		m_FileMenu.add(f_OpenItem);                
                
                // separator
		m_FileMenu.addSeparator();

                // set the "Export" option
                // set settings and add it to file menu
		JMenuItem f_ExportItem = new JMenuItem();
		f_ExportItem.setText("Export");
		f_ExportItem.setMnemonic(KeyEvent.VK_E);
                f_ExportItem.setEnabled(false);
		m_FileMenu.add(f_ExportItem);

		m_FileMenu.addSeparator();
                
                // set the "Exit" option
                // set settings and add it to file menu
		JMenuItem f_ExitItem = new JMenuItem();
		f_ExitItem.setText("Exit");
		f_ExitItem.setMnemonic(KeyEvent.VK_X);
                f_ExitItem.addActionListener(this);
		m_FileMenu.add(f_ExitItem);
	}

        // Build edit menu
	private void buildEditMenu() {
            // Edit menu and options
		m_EditMenu = new JMenu();
		m_EditMenu.setText("Edit");
		m_EditMenu.setMnemonic(KeyEvent.VK_E);

                 // set the "Undo" option
                // set settings and add it to edit menu
		JMenuItem e_UndoItem = new JMenuItem();
		e_UndoItem.setText("Undo");
		e_UndoItem.setMnemonic(KeyEvent.VK_U);
		e_UndoItem.setEnabled(false);
		m_EditMenu.add(e_UndoItem);

                // set the "Redo" option
                // set settings and add it to edit menu
		JMenuItem e_RedoItem = new JMenuItem();
		e_RedoItem.setText("Redo");
		e_RedoItem.setMnemonic(KeyEvent.VK_R);
		e_RedoItem.setEnabled(false);
		m_EditMenu.add(e_RedoItem);

                // set the "Cut" option
                // set settings and add it to edit menu
		JMenuItem e_CutItem = new JMenuItem();
		e_CutItem.setText("Cut");
		e_CutItem.setMnemonic(KeyEvent.VK_C);
		e_CutItem.setEnabled(false);
		m_EditMenu.add(e_CutItem);

                // set the "Paste" option
                // set settings and add it to edit menu
		JMenuItem e_PasteItem = new JMenuItem();
		e_PasteItem.setText("Paste");
		e_PasteItem.setMnemonic(KeyEvent.VK_P);
		e_PasteItem.setEnabled(false);
		m_EditMenu.add(e_PasteItem);

                // set the "Delete" option
                // set settings and add it to edit menu
		JMenuItem e_DeleteItem = new JMenuItem();
		e_DeleteItem.setText("Delete");
		e_DeleteItem.setMnemonic(KeyEvent.VK_D);
		e_DeleteItem.setEnabled(false);
		m_EditMenu.add(e_DeleteItem);
	}

        // build help menu
	private void buildHelpMenu() {
            // just build the damn thing
		m_HelpMenu = new JMenu();
		m_HelpMenu.setText("Help");
		m_HelpMenu.setMnemonic(KeyEvent.VK_H);

                // set the "About" option
                // set settings and add it to about menu
		JMenuItem e_AboutItem = new JMenuItem();
		e_AboutItem.setText("About");
		e_AboutItem.setMnemonic(KeyEvent.VK_A);
		e_AboutItem.setActionCommand("MN_ABOUT");
		e_AboutItem.addActionListener(this);
		m_HelpMenu.add(e_AboutItem);
	}

        // CHECK FOR ACTIONS
	@Override
	public void actionPerformed(ActionEvent e) {
            // get the source and create a temporary copy
		JMenuItem source = (JMenuItem)(e.getSource());
		// get text of the source
                String callerItem = source.getText();

		// if caller is action option, print dialog box
                if (callerItem.equals("About")) {
			JOptionPane.showMessageDialog(this.getTopLevelAncestor(), "Created by Justin S. and Trey W.", "About", JOptionPane.QUESTION_MESSAGE);
		}
                
                // if save option, call a dialog box
                // result of the dialog is the filename to save the file.
                else if (callerItem.equals("Save")) {
                    String response = JOptionPane.showInputDialog(null,
                        "Where do you want to save?",
                        "Enter file path and name:",
                        JOptionPane.QUESTION_MESSAGE);
                   
                    if (!response.isEmpty()) {
                        m_Settings.fh.save(response);
                    }
                
                // if open option, call a dialog box
                // result of the dialog is the filename to open a file
                } else if (callerItem.equals("Open")) {
                    String response = JOptionPane.showInputDialog(null,
                        "What do you want to open?",
                        "Enter file path and name:",
                        JOptionPane.QUESTION_MESSAGE);
                    
                    if (!response.isEmpty()) {
                        m_Settings.fh.open(response);
                    }
                 // if option is exit
                 // CLOSE
                } else if (callerItem.equals("Exit")) {
                        System.exit(1);
                }
	}
}